﻿using System;
using System.Data;
using System.Text;
using System.Data.SQLite;

namespace Db.DAL
{
    /// <summary>
    /// 数据访问类:shelfop
    /// </summary>
    public partial class shelfop
    {
        public shelfop()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from shelfop");
            strSql.Append(" where id=@id");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
            parameters[0].Value = id;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.shelfop Model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into shelfop(");
            strSql.Append("ShelfId,OpString)");
            strSql.Append(" values (");
            strSql.Append("@ShelfId,@OpString)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ShelfId", DbType.Int32,8),
					new SQLiteParameter("@OpString", DbType.String)};
            parameters[0].Value = Model.ShelfId;
            parameters[1].Value = Model.OpString;

            object obj = DbHelperSQLite2.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.shelfop Model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update shelfop set ");
            strSql.Append("ShelfId=@ShelfId,");
            strSql.Append("OpString=@OpString");
            strSql.Append(" where id=@id");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@ShelfId", DbType.Int32,8),
					new SQLiteParameter("@OpString", DbType.String),
					new SQLiteParameter("@id", DbType.Int32,8)};
            parameters[0].Value = Model.ShelfId;
            parameters[1].Value = Model.OpString;
            parameters[2].Value = Model.id;

            int rows = DbHelperSQLite2.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from shelfop ");
            strSql.Append(" where id=@id");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQLite2.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from shelfop ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQLite2.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.shelfop GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,ShelfId,OpString from shelfop ");
            strSql.Append(" where id=@id");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,4)
			};
            parameters[0].Value = id;

            Model.shelfop Model = new Model.shelfop();
            DataSet ds = DbHelperSQLite2.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.shelfop GetModelByLast()
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,ShelfId,OpString from shelfop ");
            strSql.Append(" order by id desc limit(0,1 )");

            Model.shelfop Model = new Model.shelfop();
            SQLiteParameter[] parameters = null;
            DataSet ds = DbHelperSQLite2.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.shelfop DataRowToModel(DataRow row)
        {
            Model.shelfop Model = new Model.shelfop();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    Model.id = int.Parse(row["id"].ToString());
                }
                if (row["ShelfId"] != null && row["ShelfId"].ToString() != "")
                {
                    Model.ShelfId = int.Parse(row["ShelfId"].ToString());
                }
                if (row["OpString"] != null)
                {
                    Model.OpString = row["OpString"].ToString();
                }
            }
            return Model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,ShelfId,OpString ");
            strSql.Append(" FROM shelfop ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM shelfop ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQLite2.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from shelfop T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "shelfop";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

